<?php
	namespace Admin\Controller;
	use  	Think\Controller;
	class UserController extends PublicController{
		
		//用户列表页
		public function index()
		{
			//实例化model类
			$user = M('user');
			//$data = $user->field()->->select();
			//echo $user->_sql();
			//$data = $user->where('u.id = d.uid')->table('__USER__ u,__USER_DETAILS__ d')->select();
			//dump($data);exit;

			//查询满足要求的的总记录数
			$count = $user->where('status=1')->count();

			//实例化分页类，传入总记录数和每页显示的记录数
			$page = new \Think\Page($count,5);



			//进行分页查询
			$list = $user->field('u.id,u.username,u.userpwd,u.nickname,u.userpic,u.level,d.gold,d.email,d.phone,d.regtime,d.lasttime,d.regip')->join('u left join __USER_DETAILS__ d on u.id=d.uid')->where('status=1')->limit($page->firstRow.','.$page->listRows)->order('u.id')->select();
				//$data = $user->select();
			//dump($list);exit;
			// $now = "<span class='now'>当前:<b>%nowPage%</b>/%totalPage%页</span>";

			// $total = "<span class='total'> 共<b>%totalRow%</b>%header%</span>";

			// $page  ="%upPage% %first% %prePage% %linkPage% %nextPage% %end% %downPage%";

			// $str = $now.' '.$total.' '.$page;
			//$page->setConfig('theme',$str);
			//查询数据库所有用户
	    	$page->setConfig('header','个会员');
	    	$page->setConfig('first','首页');
	    	$page->setConfig('last','末页');
	    	$page->setConfig('prev','上一页');
	    	$page->setConfig('next','下一页');
	    	$page->setConfig('theme',"共 %TOTAL_ROW% 条记录 %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
		

			//分页显示输出
			$show =$page->show();
			
			//赋值变量
			$this->assign('data',$list);
			$this->assign('page',$show);
			
			//输出模版
			$this->display();
		}

		//添加用户界面
		public function add()
		{
			//输出模版
			$this->display();
		}

		//执行用户添加方法
		public function insert()
		{
			//实例化model类
			$user 		= M('user');
			$details 	= M('user_details');
			
			//判断两次密码输入是否一样
			if($_POST['userpwd']==$_POST['qruserpwd']){
				$_POST['userpwd'] = md5(I('post.userpwd'));
			}else{
				$this->error('两次输入的密码不一致');
			}
		//判断用户是否上传头像
			if($file['error']!=4){

				//设置参数数组
				$config = array(
						'rootPath'=>'Public/',
						'maxSize' =>'3000000',
						'savePath'=>'Uploads/',
						'exts'	  =>array('jpg','jpeg','gif','png'),
						'saveName'=>date('YmdHis').rand(1,100000),
					);
				//实例化上传类
				$upload = new \Think\Upload($config);
				//执行上传方法
				$res = $upload->upload();
				//取到上传文件名字
				if($res){
						$_POST['userpic']= $res['userpic']['savepath'].$res['userpic']['savename'];

					}else{
						$upload->getError();
					}

			
				}
				//执行数据库插入方法
				$whl_user['username'] = $_POST['username'];
				$whl_user['userpwd'] = $_POST['userpwd'];
				$whl_user['nickname'] = $_POST['nickname'];
				$whl_user['userpic'] = $_POST['userpic'];
				$whl_user['level'] = $_POST['level']; 
				$data = $user->add($whl_user);

				//获取uid值
				$_POST['uid'] = $data;
				//插入第二张关联表
				$whl_user_details['uid'] 			= $_POST['uid'];
				$whl_user_details['gold']   		= $_POST['gold'];
				$whl_user_details['email']  		= $_POST['email'];
				$whl_user_details['phone']    		= $_POST['phone'];
				//收藏的书籍暂时用不到
				//$whl_user_details['collection']     = $_POST['collection'];
				// $whl_user_details['regtime'] 		= $_POST['regtime'];
				// $whl_user_details['lasttime'] 		= $_POST['lasttime'];
				// $whl_user_details['regip'] 			= $_POST['regip'];

				$res1 = $details->add($whl_user_details);
				if($res1){
					$this->success('添加用户成功',U('user/index'),2);
				}else{
					$this->error('添加用户失败');
				}

			}

			//垃圾站
			public function lahei()
			{
				//实例化数据库类
				$user = M('user');

				//执行修改方法
				$id = I('get.id');

				//将修改需要的条件赋值给数组
				$where['id']=I('get.id');
				$where['status'] = 0;
				//执行修改方法
				$res = $user->save($where);

				if($res){
					$this->success('禁用成功',U('User/index'),2);
				}else{
					$this->error('禁用失败');
				}


			}

			//回收站
			public function laji()
			{
				//实例化model类
				$user = M('user');

				//查询满足条件的所有条数
				$count = $user->where('status=0')->count();

				//实例化分页类
				$page = new \Think\Page($count,5);
				
				//进行关联查询
				$data = $user->field('u.id,u.username,u.userpwd,u.userpic,u.status,u.level,d.email,d.phone,d.gold,d.lasttime,d.regtime,d.regip')->join('u left join __USER_DETAILS__ d on u.id=d.uid')->where('u.status=0')->limit($page->firstRow.','.$page->listRows)->select();

				//设置分页样式
				$page->setConfig('first','首页');
				$page->setConfig('last','末页');
				$page->setConfig('prev','上一页');
				$page->setConfig('next','下一页');
				$page->setConfig('theme',"共 %TOTAL_ROW% 条记录 %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");

				//分页显示输出
				$show = $page->show();

				//赋值变量
				$this->assign(data,$data);
				$this->assign(page,$show);

				//输出页面
				$this->display();
								
			}

			//根据id查询信息并跳转到修改页面
			public function edit()
			{
				//实例化model类
				$user = M('user');
				
				$where['u.id'] = I('get.id'); 
				//echo $id;
				//根据id查询信息
				$data = $user->field('u.id,u.username,u.userpwd,u.userpic,u.status,u.level,d.email,d.phone,d.gold,d.lasttime,d.regtime,d.regip')->join('u left join __USER_DETAILS__ d on u.id=d.uid')->where($where)->find();

				//赋值变量
				$this->assign(data,$data);

				//输出模版
				$this->display();
			}


			//执行修改方法
			public function update()
			{
				//实例化model类
				$user = M('user');
				$user_details = M('user_details');
				//dump(I('post.'));
				
				//接收要修改的user表的数据
				$userb['id'] = I('post.id');
				$userb['username'] = I('post.username');
				$userb['level'] = I('post.level');
				$userb['nickname'] = I('post.nickname');
				$userpic = $_FILES['userpic']; 

				//判断是否上传了新的图像
				if($userpic['error']!=4){
					//配置文件
					$config = array(
						'rootPath'=>'Public/',
						'savePath'=>'Uploads/'
						);

					//实例化上传类
					$upload = new \Think\Upload($config);

					//执行上传方法
					$res = $upload->upload();
					//dump($res);
					$userb['userpic'] = $res['userpic']['savepath'].$res['savename'];

				}
				//dump($userb);
				//执行修改sql语句
				$res1 = $user->save($userb); 
				// echo $user->_sql();
				// dump($res1);exit;
				
				//接收第二张表数据
				$id = I('post.id');
				$user_detailss['gold'] = I('post.gold');
				$user_detailss['email'] = I('post.email');
				$user_detailss['phone'] = I('post.phone');
				//执行修改语句
				$res2 = $user_details->where("uid=$id")->save($user_detailss);
				//echo $user_details->_sql();exit;
				if($res2){
						$this->success('修改成功',U('User/index'),2);
				}else{
						$this->error('修改失败');
				}
			
			}

			//启用禁用
			public function start()
			{
				//实例化model类
				$user = M('user');

				$where['status'] = 1;
				$where['id']     =I('get.id');
				//执行修改语句
				$res = $user->save($where);
				if($res){
					$this->success('启用成功',U('User/index'),2);
				}else{
					$this->error('启用失败');
				}
			}

			


	
		
	}